<?php

class User_model extends CI_Model {

	public $email;
	public $password;
	public $name;
	public $since;
	public $role;
	public $phone;
	public $address;
	
	public function getById($id)	{
		$this->db->where('id', $id);
		$query = $this->db->get('users', 1);
		$result = $query->result();
		if (count($result) > 0)
			return $result[0];
		return null;
	}
	
	public function getPublicInfo($id)	{
		$this->db->select('id, name, address, gender, dob, avatar');
		$this->db->where('id', $id);
		$query = $this->db->get('users', 1);
		$result = $query->result();
		if (count($result) > 0)
			return $result[0];
		return null;
	}
	
	public function editPublicInfo($id)	{
		$data = array(
			'name'=>$this->name,
			'address'=>$this->address,
			'gender'=>$this->gender,
			'dob'=>$this->dob
		);
		$this->db->where('id', $id);
		return $this->db->update('users', $data);
	}
	
	public function editContactInfo($address, $phone)	{
		$data = array(
			'address'=>$address,
			'phone'=>$phone,
		);
		$this->db->where('id', $this->id);
		return $this->db->update('users', $data);
	}
	
	public function register()	{
		$this->password = sha1($this->password);
		$this->role = 'user';
		$result = $this->db->insert('users', $this);
		return $this->db->insert_id();
	}
	
	public function login()	{
		$this->db->where('email', $this->email);
		$this->db->where('password', sha1($this->password));
		$query = $this->db->get('users');
		if ($query->num_rows() > 0)	{
			$result = $query->result();
			return $result[0];
		}
		return null;
	}
	
	public function listUsers($limit = null) {
		if ($limit != null)
			$this->db->limit($limit);
		return $this->db->get('users')->result();
	}
}

?>